/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import model.Customer;
import utilities.PersistUtil;

/**
 *
 * @author Administrator
 */
public class CustomerDao {

    EntityManager em;

    public void insertCustomer(Customer customer) {

        em = PersistUtil.getEm();

        em.getTransaction().begin();
        try {

            em.persist(customer);
        } catch (Exception e) {
        }

        em.getTransaction().commit();
        System.out.println("data is inserted...");
    }

    public void updateCustomer(Customer customer) {



        em = PersistUtil.getEm();

        em.getTransaction().begin();
        try {

            em.merge(customer);
        } catch (Exception e) {
        }

        em.getTransaction().commit();
        System.out.println("data is inserted...");
    }

    public Customer findCustomer(int customerid) {

        em = PersistUtil.getEm();
        Query query = em.createQuery("select c from Customer c where c.id=:cid");
        query.setParameter("cid", customerid);
        Customer customer = (Customer) query.getSingleResult();

        return customer;

    }

    public List<Customer> getCustomerList() {
        em = PersistUtil.getEm();
        Query query = em.createQuery("select c from Customer c ");

        List<Customer> customerlist = (List<Customer>) query.getResultList();

        return customerlist;

    }
    
    public Boolean loginVal(String id,String name)
    {
    	
    	   String status="true";
    	   em = PersistUtil.getEm();
           Query query = em.createQuery("select c from Customer c where c.id=:cid and c.name=:name");
           query.setParameter("cid", id);
           query.setParameter("name", name);
           
           
           try {
        	   Customer customer = (Customer) query.getSingleResult();
		
        	   status="false";
           
           } catch (Exception e) {
        	   
        	   return false;
			// TODO: handle exception
		}
          

           return true;
    }
    
    
}
